package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {
	
	private String url="jdbc:mysql://localhost:3306/zuipinchacheng";
	private String user="root";
	private String password="root";
	private String driverName="com.mysql.jdbc.Driver";
	
	
	public Connection connction; 
	public PreparedStatement ptmt;
	public Connection getConn(){
		try {
			Class.forName(driverName);
			connction=DriverManager.getConnection(url,user,password);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return connction;
	}
	public void closeAll(ResultSet rs,Statement st,Connection con){
		try {
			if(rs!=null){
				rs.close();
			}
			if(st!=null){
				st.close();
			}
			if(con!=null){
				con.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public int exeUpdate(String sql,Object...objects){
		int num=0;
		connction=getConn();
		try {
			ptmt=connction.prepareStatement(sql);
			
			for(int i=0;i<objects.length;i++){
				ptmt.setObject(i+1, objects[i]);
			}
			num=ptmt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
				
		return num;
		
	}

	public ResultSet exeQuery(String sql,Object...objects){
		ResultSet rs=null;
		connction=getConn(); 
		try {
			ptmt=connction.prepareStatement(sql);
			for(int i=0;i<objects.length;i++){
				ptmt.setObject(i+1, objects[i]);
			}
			rs=ptmt.executeQuery();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
				
		return rs;
		
	}
	
	
	
}
